﻿#pragma once
#include "stdafx.h"
#include "GameAppDlg.h"
static int GetBindWindow(lua_State *L)
{
	CDm *dm = (CDm*)lua_topointer(L, -1);
	dm->GetBindWindow();
	lua_pushnumber(L, dm->GetBindWindow());
	return 1;
}
static int UnBindWindow(lua_State *L) {
	CDm *dm = (CDm*)lua_topointer(L, -1);
	dm->UnBindWindow();
	
	return 0;
}
static int MoveTo(lua_State *L) {
	CDm *dm = (CDm*)lua_topointer(L, -3);
	long x = lua_tonumber(L, -2);
	long y = lua_tonumber(L, -1);
	dm->MoveTo(x, y);
	return 0;
}
static int MoveToEx(lua_State *L) {
	CDm *dm = (CDm*)lua_topointer(L, -5);
	long x = lua_tonumber(L, -4);
	long y = lua_tonumber(L, -3);
	long w = lua_tonumber(L, -2);
	long h = lua_tonumber(L, -1);
	dm->MoveToEx(x, y, w, h);
	return 0;
}
static int LeftClick(lua_State *L) {
	CDm *dm = (CDm*)lua_topointer(L, -1);
	dm->Delays(100, 200);
	dm->LeftClick();
	return 0;
}
static int ClickEx(lua_State *L) {
	CDm *dm = (CDm*)lua_topointer(L, -5);
	long x = lua_tonumber(L, -4);
	long y = lua_tonumber(L, -3);
	long w = lua_tonumber(L, -2);
	long h = lua_tonumber(L, -1);
	dm->MoveTo(x + RandomNumber(0, w), y + RandomNumber(0, h));
	dm->MoveToEx(x, y, w, h);
	dm->Delays(100, 200);
	dm->LeftClick();
	return 0;
}
static int Click(lua_State *L) {
	CDm *dm = (CDm*)lua_topointer(L, -3);
	long x = lua_tonumber(L, -2);
	long y = lua_tonumber(L, -1);
	dm->Delays(100, 200);
	dm->MoveTo(x, y);
	return 0;
}
static int luaFindWindowEx(lua_State *L)//查找窗口句柄
{
	CDm *dm = (CDm*)lua_topointer(L, -3);
	CString className, title, strRet;
	className = lua_tostring(L, -2);
	title = lua_tostring(L, -1);
	long hwnd = g_dm->FindWindowExW(0, className, title);
	//lua_pushstring()
	lua_pushnumber(L, hwnd);
	return 1;
}

static int EnumProcess(lua_State *L)//枚举进程ID
{
	CDm *dm = (CDm*)lua_topointer(L, -2);
	CString strProcess, strRetProcess;
	strProcess = lua_tostring(L, -1);
	strRetProcess = dm->EnumProcess(strProcess);
	const char* cstr;
	char temp[100];
	::wsprintfA(temp, "%ls", (LPCTSTR)strRetProcess);
	cstr = temp;
	lua_pushfstring(L, cstr);
	return 1;
}
static int TerminateProcess(lua_State *L)//关闭进程
{
	CDm *dm = (CDm*)lua_topointer(L, -2);
	CString str;
	str = lua_tostring(L, -1);
	long pid = _wtol(str);
	dm->TerminateProcess(pid);
	return 0;
}
static int runScript(lua_State *L)//执行脚本
{
	int argc = lua_gettop(L);
	CDm *dm = NULL;
	CString str;
	bool ret = false;

	if (argc == 2) {
		dm = (CDm*)lua_topointer(L, -2);
		str = lua_tostring(L, -1);
		ret = dm->RunScript(str);
		lua_pushboolean(L, ret);
		return 1;
	}
	else if (argc == 3) {
		long x, y;
		dm = (CDm*)lua_topointer(L, -3);
		str = lua_tostring(L, -2);
		ret = dm->RunScript(str, &x, &y);
		lua_pushboolean(L, ret);
		lua_pushnumber(L, x);
		lua_pushnumber(L, y);
		return 3;
	}
	/*switch (argc)
	{
	case 2:
		dm = (CDm*)lua_topointer(L, -2);
		str = lua_tostring(L, -1);
		ret = dm->RunScript(str);
		lua_pushboolean(L, ret);
		return 1;
		break;
	case 3:
		long x, y;
		dm = (CDm*)lua_topointer(L, -3);
		str = lua_tostring(L, -2);
		ret = dm->RunScript(str, &x, &y);
		lua_pushboolean(L, ret);
		lua_pushnumber(L, x);
		lua_pushnumber(L, y);
		return 3;
		break;
	default:
		break;
	}*/

	return 0;
}
static int  runScripts(lua_State *L)//执行脚本
{
	int nType = lua_tonumber(L, -1);//取最后一个类型
	CString strScript, strData;
	CDm *dm = NULL;
	strData = lua_tostring(L, -2);
	strScript = lua_tostring(L, -3);
	dm = (CDm*)lua_topointer(L, -4);
	if (dm->RunScript(strScript, nType, strData)) {
		lua_pushboolean(L, true);
		return 1;
	}
	lua_pushboolean(L, false);
	return 1;
}

static int runScriptExs(lua_State *L) {
	CString strScript, strData;
	CDm *dm = NULL;
	strData = lua_tostring(L, -1);
	strScript = lua_tostring(L, -2);
	dm = (CDm*)lua_topointer(L, -3);
	CString str;
	str = dm->RunScripts(strScript, strData);
	//lua_pushboolean(L, false);
	int nLength = str.GetLength();
	int nBytes = WideCharToMultiByte(CP_ACP, 0, str, nLength, NULL, 0, NULL, NULL);
	char* VoicePath = new char[nBytes + 1];
	memset(VoicePath, 0, nLength + 1);
	WideCharToMultiByte(CP_OEMCP, 0, str, nLength, VoicePath, nBytes, NULL, NULL);
	VoicePath[nBytes] = 0;



	lua_pushfstring(L, VoicePath);
	return 1;
}
//
//static int runScriptEx(lua_State *L) {
//
//}
static int delay(lua_State *L) {
	CDm *dm = (CDm*)lua_topointer(L, -2);
	long nDelay = lua_tonumber(L, -1);
	//dm->delay(nDelay);
	Sleep(nDelay);
	return 0;
}
static int SetWindowState(lua_State *L) {
	CDm *dm = (CDm*)lua_topointer(L, -3);
	long hwnd = lua_tonumber(L, -2);
	long nFlag = lua_tonumber(L, -1);
	dm->SetWindowState(hwnd, nFlag);
	return 0;
}
static int SetBossState(lua_State *L) {
	bool bState = lua_toboolean(L, -1);
	g_BossState = bState;
	return 0;
}

static int GetBossState(lua_State *L) {
	lua_pushboolean(L, g_BossState);
	return 1;
}


static int SetState(lua_State *L) {
	bool bState = lua_toboolean(L, -1);
	g_flag = bState;
	return 0;
}

static int GetState(lua_State *L) {
	lua_pushboolean(L, g_flag);
	return 1;
}

static int SetCheckState(lua_State *L) {
	bool bState = lua_toboolean(L, -1);
	//g_flag = bState;
	CheckState = bState;
	return 0;
}


static int KeyPress(lua_State *L) {
	CDm *dm = (CDm*)lua_topointer(L, -2);
	CString str;
	str = lua_tostring(L, -1);
	dm->KeyPressChar(str);
	return 0;
}
static int KeyDownChar(lua_State *L) {
	CDm *dm = (CDm*)lua_topointer(L, -2);
	CString str;
	str = lua_tostring(L, -1);
	dm->KeyDownChar(str);
	return 0;
}
static int KeyUpChar(lua_State *L) {
	CDm *dm = (CDm*)lua_topointer(L, -2);
	CString str;
	str = lua_tostring(L, -1);
	dm->KeyUpChar(str);
	return 0;
}

static int BindWindowEx(lua_State *L) {
	CDm *dm = (CDm*)lua_topointer(L, -2);
	long hwnd = lua_tonumber(L, -1);
	int ret = dm->BindWindowEx(hwnd, L"normal", L"normal", L"normal", L"", 0);
	
	if (ret == 1)
	{
		lua_pushnumber(L, ret);
		CheckState = true;
	}
	else
	{
		lua_pushnumber(L, 0);
	}
	return 1;
}

static int luaMoveWindow(lua_State *L) {
	CDm *dm = (CDm*)lua_topointer(L, -4);
	long hwnd = lua_tonumber(L, -3);
	long x = lua_tonumber(L, -2);
	long y = lua_tonumber(L, -1);
	dm->MoveWindow(hwnd, x, y);
	return 0;
}

static int GetBossPoint(lua_State *L) {
	lua_pushnumber(L, g_nBossX);
	return 1;
}


static int ExitWls(lua_State *L) {
	CDm *dm = (CDm*)lua_topointer(L, -1);
	dm->KeyDownChar(L"right");
	dm->delay(500);
	dm->KeyDownChar(L"up");
	dm->delay(7000);
	dm->KeyUpChar(L"right");
	dm->KeyUpChar(L"up");
	dm->KeyDownChar(L"left");
	dm->delay(2000);
	dm->KeyDownChar(L"up");
	dm->delay(4000);
	dm->KeyUpChar(L"left");
	return 0;
}
static int GetItemListText(lua_State *L) {
	int nIndex = lua_tonumber(L, -2);
	int nItemIndex = lua_tonumber(L, -1);
	CString str=g_main_cwnd->m_list_info.GetItemText(nIndex, nItemIndex);
	const char * szpStr = StrP(str);
	lua_pushfstring(L, szpStr);
	return 1;
}
static int SetItemListText(lua_State *L) {//设置列表项
	CString str;
	str = lua_tostring(L, -1);
	int nIndex = lua_tonumber(L, -3);
	int nItemIndex = lua_tonumber(L, -2);
	g_main_cwnd->m_list_info.SetItemText(nIndex, nItemIndex, str);
	return 0;
}
static int SetConfig(lua_State *L) {//SetConfig(nIndex,项目名称，项目值,配置类型）
	CString str,str1, strKey;
	str = lua_tostring(L, -2);
	str1 = lua_tostring(L, -3);
	strKey= lua_tostring(L, -4);
	int nType = lua_tonumber(L, -1);
	if (nType == 0)
	{
		g_AccountFile.SetString(strKey, str1, str);
	}
	else {
		g_IniFile.SetString(strKey, str1, str);
	}
	return 0;
}

static int GetConfig(lua_State *L) {//GetConfig(nIndex,项目名,配置类型）
	CString strKey, strName;
	strName = lua_tostring(L, -2);
	strKey = lua_tostring(L, -3);
	int nType = lua_tonumber(L, -1);
	CString strData;
	if (nType == 0)
	{
		strData=g_AccountFile.GetString(strKey, strName);
	}
	else {
		strData=g_IniFile.GetString(strKey, strName);
	}
	const char * szpStr = StrP(strData);
	lua_pushfstring(L, szpStr);
	return 1;
}

//static int FindColorBlock(lua_State *L) {
//
//}